package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.entities.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @PROJECT_NAME: cloud2020
 * @DESCRIPTION: 订单业务实现
 * @Author: csc
 * @DATE: 2023/4/6 10:23
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     *下订单-->减库存-->扣余额-->该状态
     */
    @Override
    @GlobalTransactional(name = "fsp_create_group",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("---->创建订单");
        orderDao.create(order);

        log.info("---->订单微服务调用库存,做扣减count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("---->订单微服务调用库存,做扣减结束");

        log.info("---->订单微服务开始调用账户,做扣钱money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("---->订单微服务开始调用账户,做扣钱结束");

        log.info("---->修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("---->修改订单状态开结束 ");

        log.info("---->下订单结束了.哈哈哈");
    }//end





}
